package cn.flightcloud.task;

//import static net.greghaines.jesque.utils.JesqueUtils.map;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Map;
import java.util.Map.Entry;

import net.greghaines.jesque.Config;
import net.greghaines.jesque.ConfigBuilder;
import net.greghaines.jesque.Job;
import net.greghaines.jesque.client.Client;
import net.greghaines.jesque.client.ClientImpl;
import net.greghaines.jesque.utils.JesqueUtils;
import net.greghaines.jesque.worker.MapBasedJobFactory;
import net.greghaines.jesque.worker.Worker;
import net.greghaines.jesque.worker.WorkerImpl;
import net.greghaines.jesque.worker.WorkerImplFactory;

/**
 * Hello world!
 *
 */
public class App {
	public static void main(String[] args) {
		final ConfigBuilder configBuilder = new ConfigBuilder();
		try {
			URI redisUrl = new URI(System.getProperty("REDIS_PROVIDER", "127.0.0.1"));
			String redisHost = redisUrl.getHost();
			int redisPort = redisUrl.getPort();
			String redisUserInfo = redisUrl.getUserInfo();
			if (redisHost != null) {
				configBuilder.withHost(redisHost);
			}
			if (redisPort > -1) {
				configBuilder.withPort(redisPort);
			}
			if (redisUserInfo != null) {
				configBuilder.withPassword(redisUserInfo.split(":", 2)[1]);
			}
		} catch (URISyntaxException use) {
			// Handle error
		}
		final Config config = configBuilder.build();
		// Add a job to the queue
		final Job job = new Job(TestAction.class.getSimpleName(), new Object[] { 1, 2.3, true, "test", Arrays.asList("inner", 4.5) });
		final Client client = new ClientImpl(config);
		
		
		final long delay = 100; // in seconds
		final long future = System.currentTimeMillis() + (delay * 1000); // timestamp

//				client.delayedEnqueue("foo2", job, future);
//		
//		client.end();	
		
//		client.enqueue("foo", job);
		// Start a worker to run jobs from the queue
		final Worker worker = new WorkerImpl(config, Arrays.asList("foo2"),
				new MapBasedJobFactory(JesqueUtils.map(JesqueUtils.entry(TestAction.class.getSimpleName(), TestAction.class))));
		final Thread workerThread = new Thread(worker);
		workerThread.start();

		// Enqueue more jobs, etc.

		// Shutdown the worker when finished
//		worker.end(true);
//		try {
//			workerThread.join();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
	}
}
